<template>
  <div class="article-page">
    <!-- 面包屑组件 breadcrumb-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 card-->
    <el-card shadow="never" border="false">
      <!-- 按需引入 -->
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="add('add')"
          >
            添加面经
          </el-button>
        </div>
        <el-table :data="tableData" stripe style="width: 100%" v-loading='pageLoading'>
          <!-- prop数据的属性名
               label列名
       -->
          <el-table-column prop="stem" label="标题"> </el-table-column>
          <el-table-column prop="creator" label="作者"> </el-table-column>
          <el-table-column prop="likeCount" label="点赞"> </el-table-column>
          <el-table-column prop="views" label="浏览数"> </el-table-column>
          <el-table-column prop="createdAt" label="创建时间"> </el-table-column>
          <el-table-column fixed="right">
            <template v-slot:default="scope" >
              <div class="actions">
                <!-- 添加当前点击的id -->
                <i class="el-icon-view" @click="add('preview',scope.row)"></i>
                <i class="el-icon-edit-outline" @click="add('edit',scope.row)"></i>
                <i class="el-icon-delete" @click="del(scope.row)"></i>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>

    <el-drawer
      :title="title"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleClose"
      size='50%'
    >
    <!-- 如果这个状态是新增或修改 -->
     <el-form v-if="drawerType === 'add' || drawerType === 'edit'" ref="form" :model="form" :rules="rules" label-width="80px">
  <el-form-item label="标题" prop="stem">
    <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
  </el-form-item>
  <el-form-item label="内容" prop="content">
    <quillEditor  v-model="form.content" @blur="$refs.form.validateField('content')"></quillEditor>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submit()">确认</el-button>
    <!-- 点击关闭整个面板 -->
    <el-button @click="drawer = false">取消</el-button>
  </el-form-item>
</el-form>
<div class="article-preview">
   <h5>{{ form.stem }}</h5>
  <div v-html="form.content"></div>
</div>
    </el-drawer>
    <div class="block">
  <!-- <span class="demonstration">大于 7 页时的效果</span> -->
  <el-pagination
    layout="prev, pager, next,total,sizes"
    :total="total"
    :current-page.sync="params.current"
    :page-sizes.sync="params.pageSize"
    :page-size="[5,10,20,30,]"
    @current-change='getArticle'
    @size-Change='getArticle'>
  </el-pagination>
</div>
  </div>
</template>

<script>
import { getArticleApi, delArticle, addArticle, updateArticle } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  data () {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ],
      params: {
        current: 1,
        pageSize: 10
      },
      total: 0,
      // prop框的开关
      drawer: false,
      // 点击的框的状态
      drawerType: '',
      form: {},
      rules: {
        stem: [{ required: true, message: '标题必填', trigger: 'blur' }],
        content: [{ required: true, message: '内容必填', trigger: 'blur' }]
      },
      loading: false,
      pageLoading: false
    }
  },
  computed: {
    title () {
      if (this.drawerType === 'add') {
        return '新增面经'
      } else if (this.drawerType === 'edit') {
        return '修改面经'
      } else {
        return '预览面经'
      }
    }
  },
  components: {
    quillEditor
  },
  created () {
    this.getArticle()
  },
  methods: {
    // 函数名不要重名
    async getArticle () {
      // 结构出来给数据源
      const {
        data: { rows, total }
      } = await getArticleApi(this.params)
      this.tableData = rows
      this.total = total
    },
    async del (row) {
      // console.log(row)
      // 1、封装接口
      await delArticle(row.id)
      // 2、删除成功提示
      this.$message.success('删除成功')
      // 3、重新渲染
      this.getArticle()
    },
    handleClose () {
      this.drawer = false
    },
    add (val, row = {}) {
      // 开
      this.drawer = true
      // 弹框的状态
      this.drawerType = val
      // 如果当前点击的的状态是edit才执行   预览执行同样的操作
      if (this.drawerType === 'edit' || this.drawerType === 'preview') {
        // 直把对象浅拷贝   修改的时候就不会同步影响到页面上
        this.form = { ...row }
        // this.form = row  如果是直接赋值的话 是复杂数据类型，指向同一个地址 页面上也会被修改
      }
    },
    submit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 使用接口
          try {
            // 是element提供的加载转圈 给按钮添加
            this.loading = true
            // 判断点击的是谁 执行对应的代码
            if (this.drawerType === 'add') {
              await addArticle(this.form)
              this.$message.success('恭喜你添加成功！')
              // 最新数据在第一页  只有新增的时候放到第一页
              this.params.current = 1
            } else {
              // 提交编辑接口  结构出 调用
              const { id, stem, content } = this.form
              await updateArticle({ id, stem, content })
              this.$message.success('恭喜你修改成功！')
            }

            // 点完确认按钮 就关闭弹窗
            this.drawer = false
            // 重新渲染列表
            this.getArticle()
          } finally {
            // 加载完关闭
            this.loading = false
          }
        }
      })
    },
    sizeChange (val) {
      //
      this.params.pageSize = val
    },
    currentChange (val) {
      console.log(val)
    }
  },
  watch: {
    drawer (val) {
      if (!val) {
        // 需要手动清空form表单
        this.form = {}
        // form组件提供的方法 移除整个表单校验结果恢复初始值
        // 预览非空判断  因为resetFields是form提供的  如果有form就进行操作  如果没有form就不进行了
        this.$refs.form && this.$refs.form.resetFields()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
